home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / program / swagd_f.zip / FAQ.SWG / 0019_MIDI Information.pas < prev    next >
Pascal/Delphi Source File  |  1993-11-02  |  9KB  |  208 lines

  1. {
  2. ALTON PRILLAMAN
  3.  
  4. HOWEVER, <g> now would be a good time to learn about "Bitwise Operators"
  5. to accomplish your goal With minimal memory requirements.  I'll start With
  6. the basics (no offense intended).  You may have heard, or remember from a
  7. Programming class that a Byte is made up of 8 bits.  When looking at a Byte
  8. in binary, each bit holds a value of 0 or 1 that when put together in
  9. their respective places will add up to make the number.  Here's an
  10. example of a Byte:
  11.  
  12.                                B I N A R Y
  13.                                 T A B L E
  14. =========================================================================
  15.  
  16.              Power |   7   6   5   4   3   2   1   0 | of 2
  17.              ------+---------------------------------+-----
  18.              Bit # |   8   7   6   5   4   3   2   1 |
  19.              ------+---------------------------------+-----
  20.              Value | 128  64  32  16   8   4   2   1 | HEX
  21.              ------+---------------------------------+-----
  22.                 0  |   0   0   0   0   0   0   0   0 | $00
  23.                 1  |   0   0   0   0   0   0   0   1 | $01
  24.             *   2  |   0   0   0   0   0   0   1   0 | $02
  25.                 3  |   0   0   0   0   0   0   1   1 | $03
  26.             *   4  |   0   0   0   0   0   1   0   0 | $04
  27.                 5  |   0   0   0   0   0   1   0   1 | $05
  28.                 6  |   0   0   0   0   0   1   1   0 | $06
  29.                 7  |   0   0   0   0   0   1   1   1 | $07
  30.             *   8  |   0   0   0   0   1   0   0   0 | $08
  31.                 9  |   0   0   0   0   1   0   0   1 | $09
  32.                10  |   0   0   0   0   1   0   1   0 | $0A
  33.                11  |   0   0   0   0   1   0   1   1 | $0B
  34.                12  |   0   0   0   0   1   1   0   0 | $0C
  35.                13  |   0   0   0   0   1   1   0   1 | $0D
  36.                14  |   0   0   0   0   1   1   1   0 | $0E
  37.                15  |   0   0   0   0   1   1   1   1 | $0F
  38.             *  16  |   0   0   0   1   0   0   0   0 | $10
  39.                    |                                 |
  40.             *  32  |   0   0   1   0   0   0   0   0 | $20
  41.             *  64  |   0   1   0   0   0   0   0   0 | $40
  42.             * 128  |   1   0   0   0   0   0   0   0 | $80
  43.                    |                                 |
  44.               255  |   1   1   1   1   1   1   1   1 | $FF
  45.              ------+---------------------------------+-----
  46.  
  47.  * = All columns to the right had filled up With 1s, so we carried to the
  48.      next column to the left.
  49.  
  50. Notice that when all of the "bit places" have a "1" in them, that the
  51. total adds up to be 255 which is the maximum number that a Byte can hold.
  52. In binary (the inner part of the Chart), "1" is the maximum value a bit
  53. can hold Until it carries to the next column to the left.  This brings us
  54. to the next Chart, HEXIDECIMAL:
  55.  
  56.  
  57.                           H E X I D E C I M A L
  58.                                 T A B L E
  59. =========================================================================
  60.  
  61.             Power| 1   0   |of 16        Power|  1  0   |of 16
  62.           -------+---------+-----      -------+---------+-----
  63.           Decimal|         |           Decimal|         |
  64.             Value| 16  0   | HEX         Value| 16  0   | HEX
  65.           -------+---------+-----      -------+---------+-----
  66.                 0|  0  0   | $00            31|  1  1   |  $1F
  67.                 1|  0  1   | $01         *  32|  2  0   |  $20
  68.                 2|  0  2   | $02            33|  2  1   |  $21
  69.                 3|  0  3   | $03              |         |
  70.                 4|  0  4   | $04            47|  2  F   |  $2F
  71.                 5|  0  5   | $05         *  48|  3  0   |  $30
  72.                 6|  0  6   | $06            63|  3  F   |  $3F
  73.                 7|  0  7   | $07         *  64|  4  0   |  $40
  74.                 8|  0  8   | $08            79|  4  F   |  $4F
  75.                 9|  0  9   | $09            80|  5  0   |  $50
  76.                10|  0  A   | $0A            95|  5  F   |  $5F
  77.                11|  0  B   | $0B         *  96|  6  0   |  $60
  78.                12|  0  C   | $0C           111|  6  F   |  $6F
  79.                13|  0  D   | $0D         * 112|  7  0   |  $70
  80.                14|  0  E   | $0E           127|  7  F   |  $7F
  81.                15|  0  F   | $0F         * 128|  8  0   |  $80
  82.            *   16|  1  0   | $10           255|  F  F   |  $FF
  83.                17|  1  1   | $11         * 256|         |$0100
  84.           -------+---------+-----      -------+---------+-----
  85.  
  86.  * = All columns to the right had filled up With 15 (F) so we carried
  87.      to the next column to the left.
  88.  
  89. The hexidecimal table is derived from BASE 16.  The value that each column
  90. may hold a value of 15 (F) before we carry to the next column.  Also
  91. notice that when both columns fill up With a value of "F" ($FF) that the
  92. result is 255, which is the maximum For a Byte.
  93.  
  94.  
  95. Okay, With that behind us, let's take a look at your application.  As you
  96. may have noticed in the binary table in the previous message, a Byte will
  97. give us the ability to track up to 8 bits.  Our goal here is to turn on or
  98. off each of the 8 bits as each channel is turned on or off.  I assume that
  99. you've got 16 channels to work With, so we'll use a Word instead of a
  100. Byte.  When looked at in binary, a Word is like placing two Bytes
  101. side-by-side.  Notice that the HEXIDECIMAL works the same way.
  102.  
  103.    256-------------------------+  +---------------------------- 128
  104.    512----------------------+  |  |  +-------------------------  64
  105.   1024-------------------+  |  |  |  |  +----------------------  32
  106.   2048----------------+  |  |  |  |  |  |  +-------------------  16
  107.   4096-------------+  |  |  |  |  |  |  |  |  +----------------   8
  108.   8192----------+  |  |  |  |  |  |  |  |  |  |  +-------------   4
  109.  16384-------+  |  |  |  |  |  |  |  |  |  |  |  |  +----------   2
  110.  32768----+  |  |  |  |  |  |  |  |  |  |  |  |  |  |  +-------   1
  111.           |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |
  112.           |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |
  113.  Power | 15 14 13 12 11 10  9  8  7  6  5  4  3  2  1  0|of 2
  114. -------+------------------------------------------------+-------
  115.  Bit # | 16 15 14 13 12 11 10  9  8  7  6  5  4  3  2  1|
  116. -------+------------------------------------------------+-------
  117. Decimal|                                                |
  118.   Value|                   BINARY                       |   HEX
  119. -------+------------------------------------------------+-------
  120.       1|  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  1| $0001
  121.       2|  0  0  0  0  0  0  0  0  0  0  0  0  0  0  1  0| $0002
  122.       4|  0  0  0  0  0  0  0  0  0  0  0  0  0  1  0  0| $0004
  123.       8|  0  0  0  0  0  0  0  0  0  0  0  0  1  0  0  0| $0008
  124.      16|  0  0  0  0  0  0  0  0  0  0  0  1  0  0  0  0| $0010
  125.      32|  0  0  0  0  0  0  0  0  0  0  1  0  0  0  0  0| $0020
  126.      64|  0  0  0  0  0  0  0  0  0  1  0  0  0  0  0  0| $0040
  127.     128|  0  0  0  0  0  0  0  0  1  0  0  0  0  0  0  0| $0080
  128.     256|  0  0  0  0  0  0  0  1  0  0  0  0  0  0  0  0| $0100
  129.     512|  0  0  0  0  0  0  1  0  0  0  0  0  0  0  0  0| $0200
  130.    1024|  0  0  0  0  0  1  0  0  0  0  0  0  0  0  0  0| $0400
  131.    2048|  0  0  0  0  1  0  0  0  0  0  0  0  0  0  0  0| $0800
  132.    4096|  0  0  0  1  0  0  0  0  0  0  0  0  0  0  0  0| $1000
  133.    8192|  0  0  1  0  0  0  0  0  0  0  0  0  0  0  0  0| $2000
  134.   16384|  0  1  0  0  0  0  0  0  0  0  0  0  0  0  0  0| $4000
  135.   32768|  1  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0| $8000
  136. -------+------------------------------------------------+-------
  137.  
  138. Though it has taken us a While to get here, you now have a "value" for
  139. each of your midi channels.  if you need to use more than 16 channels, you
  140. can use the same methods applied above using a LongInt to give you a total
  141. of 32 channels (or bits).
  142.  
  143. You can now declare these as Constants in your Program like so:
  144. }
  145.  
  146. Program MidiStuff;
  147. Const
  148.    {Midi Channels}
  149.    Ch1  = $0001;
  150.    Ch2  = $0002;
  151.    Ch3  = $0004;
  152.    Ch4  = $0008;
  153.    Ch5  = $0010;
  154.    Ch6  = $0020;
  155.    Ch7  = $0040;
  156.    Ch8  = $0080;
  157.    Ch9  = $0100;
  158.    Ch10 = $0200;
  159.    Ch11 = $0400;
  160.    Ch12 = $0800;
  161.    Ch13 = $1000;
  162.    Ch14 = $2000;
  163.    Ch15 = $4000;
  164.    Ch16 = $8000;
  165.  
  166. Var
  167.   MidiChannels : Word;
  168.  
  169. { Now you can turn on or off each channel and check to see if one is set by
  170. using the following Procedures and Functions.  You can accomplish this by
  171. using the or and and operators. }
  172.  
  173. Function ChannelIsOn(Ch : Word) : Boolean;
  174. begin
  175.    ChannelIsOn := (MidiChannels and Ch = Ch);
  176. end;
  177.  
  178. Procedure TurnOnChannel(Ch : Word);
  179. begin
  180.    MidiChannels := MidiChannels or Ch;
  181. end;
  182.  
  183. Procedure TurnOffChannel(Ch : Word);
  184. begin
  185.    MidiChannels := MidiChannels and not Ch;
  186. end;
  187.  
  188. begin
  189.    MidiChannels := $0000; {Initialize MidiChannels - No channels on!}
  190.    TurnOnChannel(Ch2);
  191.    if ChannelIsOn(Ch2) then
  192.      Writeln('Channel 2 is on!')
  193.    else
  194.      Writeln('Channel 2 is off!');
  195.    if ChannelIsOn(Ch3) then
  196.      Writeln('Channel 3 is on!')
  197.    else
  198.      Writeln('Channel 3 is off!');
  199.    TurnOnChannel(Ch16);
  200.    TurnOnChannel(Ch10);
  201.    TurnOffChannel(Ch2);
  202.    if ChannelIsOn(Ch2) then
  203.      Writeln('Channel 2 is on!')
  204.    else
  205.      Writeln('Channel 2 is off!');
  206. end.
  207.  
  208.